package com.wyix.log.config;

import com.wyix.log.excutor.Slf4jLogEnhancer;
import com.wyix.log.properties.MilkTeaLogProperties;
import com.wyix.log.service.MilkTeaLogService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author wangdx
 * @since 2025/8/20
 */
// 自动配置"大脑"
@Configuration
@ConditionalOnClass(MilkTeaLogService.class)
@EnableConfigurationProperties(MilkTeaLogProperties.class)
public class MilkTeaLogAutoConfiguration {
    private final MilkTeaLogProperties properties;

    public MilkTeaLogAutoConfiguration(MilkTeaLogProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "milktea.log.enabled", havingValue = "true", matchIfMissing = true)
    public MilkTeaLogService milkTeaLogService() {
        MilkTeaLogService service = new MilkTeaLogService();
        service.setLevel(properties.getLevel());
        service.setPrintIngredients(properties.isPrintIngredients());
        return service;
    }

    @Bean
    @ConditionalOnClass(name = "org.slf4j.Logger")
    @ConditionalOnBean(MilkTeaLogService.class)
    public Slf4jLogEnhancer slf4jLogEnhancer(MilkTeaLogService logService) {
        return new Slf4jLogEnhancer(logService);
    }
}
